﻿using Learun.Util;
using Learun.Util.SqlSugar;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using DbType = System.Data.DbType;

namespace Learun.Application.Base.CodeSchemaModule
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2019-03-01 11:09
    /// 描 述：代码模板
    /// </summary>
    public class CodeSchemaService
    {
        #region 仓储
        Repository<LR_Base_CodeSchemaEntity> _codeSchemaRepository => new Repository<LR_Base_CodeSchemaEntity>();
        #endregion
        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        public IEnumerable<LR_Base_CodeSchemaEntity> GetPageList(Pagination pagination, string queryJson)
        {
            try
            {
                var strSql = new StringBuilder();
                strSql.Append("SELECT ");
                strSql.Append(@"
                t.F_Id,
                t.F_Name,
                t.F_Catalog,
                t.F_Type,
                t.F_Description
                ");
                strSql.Append("  FROM LR_Base_CodeSchema t ");
                strSql.Append("  WHERE 1=1 ");
                var queryParam = queryJson.ToJObject();
                // 虚拟参数
                //var dp = new DynamicParameters(new { });
                var dp = new List<SugarParameter>();
                if (!queryParam["F_Name"].IsEmpty())
                {
                    dp.Add(new SugarParameter("F_Name", "%" + queryParam["F_Name"].ToString() + "%", DbType.String));
                    strSql.Append(" AND t.F_Name Like @F_Name ");
                }
                if (!queryParam["F_Catalog"].IsEmpty())
                {
                    dp.Add(new SugarParameter("F_Catalog",queryParam["F_Catalog"].ToString(), DbType.String));
                    strSql.Append(" AND t.F_Catalog = @F_Catalog ");
                }
                //return this.BaseRepository().FindList<LR_Base_CodeSchemaEntity>(strSql.ToString(),dp, pagination);
                return SqlSugarHelper.Db.SqlQueryable<LR_Base_CodeSchemaEntity>(strSql.ToString()).AddParameters(dp).ToPageList(pagination.page, pagination.rows);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取LR_Base_CodeSchema表实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public LR_Base_CodeSchemaEntity GetLR_Base_CodeSchemaEntity(string keyValue)
        {
            try
            {
                return _codeSchemaRepository.GetById(keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 获取树形数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSqlTree()
        {
            try
            {
                //return this.BaseRepository().FindTable(" select * from lr_base_dataitemdetail ");
                return SqlSugarHelper.Db.Ado.GetDataTable(" select * from lr_base_dataitemdetail ");
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

        #region 提交数据

        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void DeleteEntity(string keyValue)
        {
            try
            {
                _codeSchemaRepository.Delete(t=>t.F_Id == keyValue);
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        public void SaveEntity(string keyValue, LR_Base_CodeSchemaEntity entity)
        {
            try
            {
                if (!string.IsNullOrEmpty(keyValue))
                {
                    entity.Modify(keyValue);
                    _codeSchemaRepository.Update(entity);
                }
                else
                {
                    entity.Create();
                    _codeSchemaRepository.Insert(entity);
                }
            }
            catch (Exception ex)
            {
                if (ex is ExceptionEx)
                {
                    throw;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(ex);
                }
            }
        }

        #endregion

    }
}
